set -x
cnpm=npm\ --registry=https://registry.npm.taobao.org\ --cache=~/.npm/.cache/cnpm\ --disturl=https://npm.taobao.org/dist\ --userconfig=~/.cnpmrc
git_init(){
    git remote add origin https://gitee.com/blackfur/bash.git
    git push -u origin master
}

egrep_check(){
    tt=`date +"%s"`
    cat > $tt <<EOF
foo
EOF
    cat $tt
    egrep $'(\t|^)foo' $tt
    rm $tt
}
cut_check(){
    echo $RANDOM | cut -c1
    echo "wk company" | cut -c1-4
}

daemon(){
    vanish

    boot_cmd=$@
    rm -rf $PWD/log
    mkdir -p log
    log_file=log/`date +"%F_%T"`.log
    nohup $boot_cmd >$log_file 2>&1 &
    echo $$ >$PWD/pid
    sleep 2
    tail $log_file
}

vanish(){
    kill_port $1
    kill_pid
}

kill_pid(){
    if [[ -s "pid" ]];then
	pid=`cat pid`
	if [ ! -z "$pid" ]
	then
	    kill -9 $pid
	fi
    fi
    sleep 2
}

kill_port(){
    [ ! -z "$1" ] && kill -9 $(lsof -t -i:$1)
    if [[ -s "port" ]];then
	port=`cat port`
	if [ ! -z "$port" ]
	then
	    kill -9 $(lsof -t -i:$port)
	fi
    fi
    sleep 2
}

if [[ "$1" = "--check" ]]
then
    shift
    $@
    exit
fi
